home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Programming / MiniGL / src / others.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-04-11  |  9.1 KB  |  366 lines

  1. /*
  2.  * $Id: others.c,v 1.1.1.1 2000/04/07 19:44:51 hfrieden Exp $
  3.  *
  4.  * $Date: 2000/04/07 19:44:51 $
  5.  * $Revision: 1.1.1.1 $
  6.  *
  7.  * (C) 1999 by Hyperion
  8.  * All rights reserved
  9.  *
  10.  * This file is part of the MiniGL library project
  11.  * See the file Licence.txt for more details
  12.  *
  13.  */
  14.  
  15. #include "sysinc.h"
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18.  
  19. static char rcsid[] = "$Id: others.c,v 1.1.1.1 2000/04/07 19:44:51 hfrieden Exp $";
  20.  
  21.  
  22. #ifndef __PPC__
  23. extern struct IntuitionBase *IntuitionBase;
  24. extern struct ExecBase *SysBase;
  25. #endif
  26.  
  27. void GLAlphaFunc(GLcontext context, GLenum func, GLclampf ref)
  28. {
  29.     ULONG w3dmode;
  30.     W3D_Float refvalue = (W3D_Float)ref;
  31.  
  32.     GLFlagError(context, context->CurrentPrimitive != GL_BASE, GL_INVALID_OPERATION);
  33.  
  34.     switch(func)
  35.     {
  36.         case GL_NEVER:
  37.             w3dmode = W3D_A_NEVER;
  38.             break;
  39.         case GL_LESS:
  40.             w3dmode = W3D_A_LESS;
  41.             break;
  42.         case GL_EQUAL:
  43.             w3dmode = W3D_A_EQUAL;
  44.             break;
  45.         case GL_LEQUAL:
  46.             w3dmode = W3D_A_LEQUAL;
  47.             break;
  48.         case GL_GREATER:
  49.             w3dmode = W3D_A_GREATER;
  50.             break;
  51.         case GL_NOTEQUAL:
  52.             w3dmode = W3D_A_NOTEQUAL;
  53.             break;
  54.         case GL_GEQUAL:
  55.             w3dmode = W3D_A_GEQUAL;
  56.             break;
  57.         case GL_ALWAYS:
  58.             w3dmode = W3D_A_ALWAYS;
  59.             break;
  60.         default:
  61.             GLFlagError(context, 1, GL_INVALID_ENUM);
  62.             break;
  63.     }
  64.  
  65.     W3D_SetAlphaMode(context->w3dContext, w3dmode, &refvalue);
  66.  
  67. }
  68.  
  69. void GLDrawBuffer(GLcontext context, GLenum mode)
  70. {
  71. }
  72.  
  73. void GLPolygonMode(GLcontext context, GLenum face, GLenum mode)
  74. {
  75. }
  76.  
  77. void GLShadeModel(GLcontext context, GLenum mode)
  78. {
  79.     //LOG(2, glShadeModel, "%d", mode);
  80.     if (mode == GL_FLAT)
  81.     {
  82.         W3D_SetState(context->w3dContext, W3D_GOURAUD, W3D_DISABLE);
  83.         context->ShadeModel = mode;
  84.     }
  85.     else if (mode == GL_SMOOTH)
  86.     {
  87.         W3D_SetState(context->w3dContext, W3D_GOURAUD, W3D_ENABLE);
  88.         context->ShadeModel = mode;
  89.     }
  90. }
  91.  
  92.  
  93.  
  94. #define BLS(X) case GL_##X: src=W3D_##X; break
  95. #define BLD(X) case GL_##X: dest=W3D_##X; break
  96.  
  97. void GLBlendFunc(GLcontext context, GLenum sfactor, GLenum dfactor)
  98. {
  99.     ULONG src, dest;
  100.     switch(sfactor)
  101.     {
  102.         BLS(ZERO);
  103.         BLS(ONE);
  104.         BLS(DST_COLOR);
  105.         BLS(ONE_MINUS_DST_COLOR);
  106.         BLS(SRC_ALPHA);
  107.         BLS(ONE_MINUS_SRC_ALPHA);
  108.         BLS(DST_ALPHA);
  109.         BLS(ONE_MINUS_DST_ALPHA);
  110.         BLS(SRC_ALPHA_SATURATE);
  111.     default:
  112.         GLFlagError(context, 1, GL_INVALID_ENUM);
  113.     }
  114.  
  115.     switch(dfactor)
  116.     {
  117.         BLD(ZERO);
  118.         BLD(ONE);
  119.         BLD(SRC_COLOR);
  120.         BLD(ONE_MINUS_SRC_COLOR);
  121.         BLD(SRC_ALPHA);
  122.         BLD(ONE_MINUS_SRC_ALPHA);
  123.         BLD(DST_ALPHA);
  124.         BLD(ONE_MINUS_DST_ALPHA);
  125.     }
  126.     // Try to set the mode, if unavailable, switch to
  127.     // (SRC_ALPHA, ONE_MINUS_SRC_ALPHA) which is supported
  128.     // by almost all Amiga supported graphics cards
  129.     if (W3D_SetBlendMode(context->w3dContext, src, dest) == W3D_UNSUPPORTEDBLEND)
  130.     {
  131.         if (context->NoFallbackAlpha == GL_FALSE)
  132.         {
  133.             W3D_SetBlendMode(context->w3dContext, W3D_SRC_ALPHA, W3D_ONE_MINUS_SRC_ALPHA);
  134.             context->AlphaFellBack = GL_TRUE;
  135.         }
  136.         else
  137.         {
  138.             context->AlphaFellBack = GL_FALSE;
  139.         }
  140.     }
  141.     else
  142.     {
  143.         context->AlphaFellBack = GL_FALSE;
  144.     }
  145.  
  146.     context->SrcAlpha = sfactor;
  147.     context->DstAlpha = dfactor;
  148. }
  149.  
  150. void GLHint(GLcontext context, GLenum target, GLenum mode)
  151. {
  152.     ULONG hint;
  153.     switch(mode)
  154.     {
  155.         case GL_FASTEST:    hint = W3D_H_FAST; break;
  156.         case GL_NICEST:     hint = W3D_H_NICE; break;
  157.         case GL_DONT_CARE:  hint = W3D_H_AVERAGE; break;
  158.         default:
  159.             GLFlagError(context, 1, GL_INVALID_ENUM);
  160.             break;
  161.     }
  162.  
  163.     switch(target)
  164.     {
  165.         case GL_FOG_HINT:
  166.             W3D_Hint(context->w3dContext, W3D_H_FOGGING, hint);
  167.             break;
  168.         case GL_PERSPECTIVE_CORRECTION_HINT:
  169.             W3D_Hint(context->w3dContext, W3D_H_PERSPECTIVE, hint);
  170.             break;
  171.         case MGL_W_ONE_HINT:
  172.             if (mode == GL_FASTEST) context->WOne_Hint = GL_TRUE;
  173.             else            context->WOne_Hint = GL_FALSE;
  174.             break;
  175.         default:
  176.             GLFlagError(context, 1, GL_INVALID_ENUM);
  177.     }
  178. }
  179.  
  180. const GLubyte * GLGetString(GLcontext context, GLenum name)
  181. {
  182.     switch(name)
  183.     {
  184.         case GL_RENDERER:
  185.             if (context->w3dContext)
  186.             {
  187.                 switch(context->w3dContext->CurrentChip)
  188.                 {
  189.                     case W3D_CHIP_VIRGE:
  190.                         return "MiniGL/Warp3D S3 ViRGE (virge)";
  191.                     case W3D_CHIP_PERMEDIA2:
  192.                         return "MiniGL/Warp3D 3DLabs Permedia 2 (permedia)";
  193.                     case W3D_CHIP_VOODOO1:
  194.                         return "MiniGL/Warp3D 3D/fx Voodoo 1 (voodoo)";
  195.                     case W3D_CHIP_UNKNOWN:
  196.                         return "MiniGL/Warp3D Unknown graphics chip";
  197.                     default:
  198.                         return "MiniGL/Warp3D";
  199.                 }
  200.             }
  201.             else
  202.             {
  203.                 return "MiniGL/Warp3D";
  204.             }
  205.  
  206.         case GL_VENDOR:     return "Hyperion";
  207.         case GL_VERSION:    return "1.1";
  208.         case GL_EXTENSIONS: return "";
  209.         default:            return "Huh?";
  210.     }
  211. }
  212.  
  213. void GLGetFloatv(GLcontext context, GLenum pname, GLfloat *params)
  214. {
  215.     int i;
  216.  
  217.     switch(pname)
  218.     {
  219.         case GL_MODELVIEW_MATRIX:
  220.             for (i=0; i<16; i++)
  221.             {
  222.                 *params = context->ModelView[context->ModelViewNr].v[i];
  223.                 params++;
  224.             }
  225.             return;
  226.         default:
  227.             GLFlagError(context, 1, GL_INVALID_ENUM);
  228.             return;
  229.     }
  230. }
  231.  
  232. GLenum GLGetError(GLcontext context)
  233. {
  234.     GLenum ret = context->CurrentError;
  235.     context->CurrentError = GL_NO_ERROR;
  236.     return ret;
  237. }
  238.  
  239. void GLReadPixels(GLcontext context, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid *pixels)
  240. {
  241.     // NYI
  242. }
  243.  
  244. void MGLSetZOffset(GLcontext context, GLfloat offset)
  245. {
  246.     context->ZOffset = offset;
  247. }
  248.  
  249. void MGLWriteShotPPM(GLcontext context, char *filename)
  250. {
  251.     GLubyte *pixelline;
  252.     FILE *f;
  253.     int i;
  254.     size_t bytes;
  255.  
  256.     pixelline = (GLubyte *)malloc(context->w3dWindow->Width*3);
  257.     if (!pixelline) return;
  258.  
  259.     f = fopen(filename, "wb");
  260.     if (!f)
  261.     {
  262.         free(pixelline);
  263.         return;
  264.     }
  265.  
  266.     // Write PPM header
  267.     fprintf(f, "P6\n%d %d\n255\n", context->w3dWindow->Width,
  268.     context->w3dWindow->Height);
  269.  
  270.     for (i=0; i<context->w3dWindow->Height; i++)
  271.     {
  272.         (void)ReadPixelArray(pixelline, 0, 0, context->w3dWindow->Width, context->w3dWindow->RPort,
  273.             0, (UWORD)i, context->w3dWindow->Width, 1, RECTFMT_RGB);
  274.         bytes = fwrite(pixelline, context->w3dWindow->Width*3, 1, f);
  275.     }
  276.  
  277.     fclose(f);
  278.     free(pixelline);
  279. }
  280.  
  281. void MGLKeyFunc(GLcontext context, KeyHandlerFn k)
  282. {
  283.     context->KeyHandler = k;
  284. }
  285.  
  286. void MGLSpecialFunc(GLcontext context, SpecialHandlerFn s)
  287. {
  288.     context->SpecialHandler = s;
  289. }
  290.  
  291. void MGLMouseFunc(GLcontext context, MouseHandlerFn m)
  292. {
  293.     context->MouseHandler = m;
  294. }
  295.  
  296. void MGLIdleFunc(GLcontext context, IdleFn i)
  297. {
  298.     context->Idle = i;
  299. }
  300.  
  301. void MGLExit(GLcontext context)
  302. {
  303.     context->Running = GL_FALSE;
  304. }
  305.  
  306. void MGLMainLoop(GLcontext context)
  307. {
  308.     // FIXME: This should become 68k
  309.     struct IntuiMessage *imsg;
  310.     struct Window *window;
  311.     ULONG Class;
  312.     UWORD Code;
  313.     WORD MouseX, MouseY;
  314.     GLbitfield buttons = 0;
  315.  
  316.     window = (struct Window *)mglGetWindowHandle();
  317.     ModifyIDCMP(window, IDCMP_VANILLAKEY|IDCMP_RAWKEY|IDCMP_MOUSEMOVE|IDCMP_MOUSEBUTTONS);
  318.  
  319.     context->Running = GL_TRUE;
  320.  
  321.     while (context->Running == GL_TRUE)
  322.     {
  323.         while (imsg = (struct IntuiMessage *)GetMsg(window->UserPort))
  324.         {
  325.             Class  = imsg->Class;
  326.             Code   = imsg->Code;
  327.             MouseX = imsg->MouseX;
  328.             MouseY = imsg->MouseY;
  329.             ReplyMsg((struct Message *)imsg);
  330.             switch(Class)
  331.             {
  332.             case IDCMP_VANILLAKEY:
  333.                 if (context->KeyHandler)
  334.                 {
  335.                     context->KeyHandler((char)Code);
  336.                 }
  337.                 break;
  338.             case IDCMP_MOUSEBUTTONS:
  339.                 switch(Code)
  340.                 {
  341.                     case SELECTDOWN: buttons |= MGL_BUTTON_LEFT;    break;
  342.                     case SELECTUP:   buttons &= ~MGL_BUTTON_LEFT;   break;
  343.                     case MENUDOWN:   buttons |= MGL_BUTTON_RIGHT;   break;
  344.                     case MENUUP:     buttons &= ~MGL_BUTTON_RIGHT;  break;
  345.                     case MIDDLEDOWN: buttons |= MGL_BUTTON_MID;     break;
  346.                     case MIDDLEUP:   buttons &= MGL_BUTTON_MID;     break;
  347.                 }
  348.             // drop through
  349.             case IDCMP_MOUSEMOVE:
  350.                 if (context->MouseHandler)
  351.                 {
  352.                     context->MouseHandler((GLint)MouseX, (GLint)MouseY, buttons);
  353.                 }
  354.                 break;
  355.             } /* switch Class */
  356.         } /* While imsg */
  357.  
  358.         if (context->Idle)
  359.         {
  360.             context->Idle();
  361.         }
  362.     } /* While running */
  363. }
  364.  
  365.  
  366.